#include "thread.h"
#include <QDebug>
#include <QMutex>
#include <QWaitCondition>

QMutex mutex;
QWaitCondition empty, full;
int num=0;
int buffer[50];
int useByte=0;


Producer::Producer()
{

}
void Producer::run()
{
	for(int i=0; i<50; i++)
	{
		mutex.lock();
		if(useByte==50)
			empty.wait(&mutex);
		num++;
		buffer[i] = num;
		qDebug()<<"producer:"<<num;
		useByte++;
		full.wakeAll();
		mutex.unlock();
		sleep(1);
	}
}
Consumer::Consumer()
{

}
void Consumer::run()
{
	for(int i=0; i<50; i++)
	{
		mutex.lock();
		if(useByte==0)
			full.wait(&mutex);
		qDebug()<<"consumer"<<buffer[i];
		useByte--;
		empty.wakeAll();
		mutex.unlock();
		sleep(2);
	}
}
